package com.lenovo.smarttraffic.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lenovo.smarttraffic.Constant;
import com.lenovo.smarttraffic.MainActivity;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.CarInfo;
import com.lenovo.smarttraffic.bean.UserBean;
import com.lenovo.smarttraffic.ui.activity.stage2.AllRechargeActivity;
import com.lenovo.smarttraffic.util.HttpUtils;
import com.lenovo.smarttraffic.util.Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AccountChargeAdapter extends BaseAdapter {

    private CarInfo account;
    private UserBean user;

    private Context context;

    private SharedPreferences preferences;

    private SparseBooleanArray sparseBooleanArray = new SparseBooleanArray();
    private List<CarInfo.ROWSDETAILBean> mList = new ArrayList<>();

    public AccountChargeAdapter(Context context, CarInfo account, UserBean user) {
        this.context = context;
        this.account = account;
        this.user = user;
        this.preferences = context.getSharedPreferences(Constant.THRESHOLD, Context.MODE_PRIVATE);
    }

    @Override
    public int getCount() {
        return 4;
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item_account_manage, parent, false);
            holder = new ViewHolder();
            holder.parent = convertView.findViewById(R.id.item_parent);
            holder.carId = convertView.findViewById(R.id.car_id);
            holder.car_img = convertView.findViewById(R.id.car_img);
            holder.title = convertView.findViewById(R.id.title);
            holder.value = convertView.findViewById(R.id.value);
            holder.money = convertView.findViewById(R.id.money);
            holder.checkBox = convertView.findViewById(R.id.checkbox);
            holder.charge_btn = convertView.findViewById(R.id.charge_btn);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (account != null && account.getROWS_DETAIL() != null && account.getROWS_DETAIL().size() > 0
                && user.getROWS_DETAIL() != null && user.getROWS_DETAIL().size() > 0) {

            CarInfo.ROWSDETAILBean rowsdetailBean = account.getROWS_DETAIL().get(position);
            UserBean.ROWS_DETAIL rowsdetailBean2 = user.getROWS_DETAIL().get(position);

            HttpUtils.post(context, "get_car_account_balance",
                    new HttpUtils().setParam("CarId", position + 1)
                            .setParam("UserName", MainActivity.userName),
                    response -> {
                        Map responseMap = new Gson().fromJson(response.toString(), Map.class);
                        int money = (int) Utils.parseDouble(responseMap.get("Balance").toString());
                        boolean needAlert = money < Utils.parseInt(preferences.getString(Constant.ts_1_4, "-1"));
                        holder.parent.setBackgroundColor(Utils.getColor(context, needAlert ? R.color.Yellow : R.color.White));
                        holder.money.setText(String.format("余额：%s元", money));
                    });


            holder.carId.setText(String.valueOf(rowsdetailBean.getNumber()));

            holder.car_img.setBackgroundResource(Utils.getCarBandRes(rowsdetailBean.getCarbrand()));

            holder.title.setText(rowsdetailBean.getCarnumber());

            holder.value.setText(String.format("车主：%s", rowsdetailBean2.getPname()));

            holder.checkBox.setTag(position);
            holder.checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
                //读取当前的view的tag
                int tag = (int) buttonView.getTag();
                //判读是否选择
                if (isChecked) {
                    //选择了就将状态存入
                    sparseBooleanArray.put(tag, true);
                    //将选中的item数据保存出来
                    mList.add(account.getROWS_DETAIL().get(position));
                } else {
                    //不然就删除
                    sparseBooleanArray.delete(tag);
                    mList.remove(account.getROWS_DETAIL().get(position));
                }
            });


            holder.checkBox.setChecked(sparseBooleanArray.get(position, false));

            holder.charge_btn.setOnClickListener(v -> {
                Intent intent = new Intent(context, AllRechargeActivity.class);
                if (sparseBooleanArray.size() > 0) {
                    String carInfo = new Gson().toJson(mList);
                    intent.putExtra("carInfo", carInfo);
                } else {
                    List<CarInfo.ROWSDETAILBean> data = new ArrayList<>();
                    data.add(account.getROWS_DETAIL().get(position));

                    String carInfo = new Gson().toJson(data);
                    intent.putExtra("carInfo", carInfo);
                    intent.putExtra("pos", position + 1);
                }
                context.startActivity(intent);
            });
        }

        return convertView;
    }

    static class ViewHolder {
        LinearLayout parent;
        TextView carId, title, value, money;
        View car_img;
        CheckBox checkBox;
        Button charge_btn;
    }
}
